package com.wbao.dianniu.im;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.data.NoticeSystemData;
import com.wbao.dianniu.listener.INoticeSystemListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.manager.ChatNotifyReadManager;
import com.wbao.dianniu.manager.NoticeSystemListManager;
import com.wbao.dianniu.ui.BaseActivity;
import com.wbao.dianniu.R;
import com.wbao.dianniu.update.ChatUnreadChangeManger;
import com.wbao.dianniu.utils.DateUtils;

/**
 * 电纽助手
 *
 * @author yejianjun
 */
public class DnHelperActivity extends BaseActivity implements INoticeSystemListListener {
    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;
    private PullToRefreshListView pullListView;
    private ListView mListView;
    private NoticeSystemListManager manager;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;
    private ChatNotifyReadManager chatNotifyReadManager;
    private MyAdapter adapter;
    private boolean IsPullDown = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_dn_helper, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        int unread = getIntent().getIntExtra(Const.BUNDLE_COUNT, 0);
        String title = getIntent().getStringExtra(Const.BUNDLE_POST);
        setTitleName(title == null ? "" : title);
        ChatUnreadChangeManger.getInstance().minusNotifyUnread(unread);
        ChatUnreadChangeManger.getInstance().chatUnreadChange(114, 0);
        initWattingView();
        init();
        initData();
    }

    private void initWattingView() {
        waittingLayout = findViewById(R.id.waitting_layout);
        waittingProBar = findViewById(R.id.waitting_progress);
        waittingTV = findViewById(R.id.waitting_text_desc);
    }

    private void initData() {
        chatNotifyReadManager = new ChatNotifyReadManager(this);
        chatNotifyReadManager.chatNotifyRead(114);
        ChatUnreadChangeManger.getInstance().chatUnreadChange(114, 0);
        manager = new NoticeSystemListManager(this);
        manager.addNoticeSystemListListener(this);
        reqData(currentPage);
    }

    private void reqData(int page) {
        manager.noticeSystemList(page * PAGE_COUNTS, PAGE_COUNTS);
    }

    private void init() {
        pullListView = findViewById(R.id.agent_listview);
        pullListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullListView.setOnRefreshListener(onRefreshListener2);
        mListView = pullListView.getRefreshableView();
        registerForContextMenu(mListView);

        adapter = new MyAdapter(this);
        adapter.addData(new ArrayList<NoticeSystemData>());
        mListView.setAdapter(adapter);
    }

    PullToRefreshBase.OnRefreshListener2 onRefreshListener2 = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh() {
            pullDownRequest();
        }

        @Override
        public void onPullUpToRefresh() {
            pullUpRequest();
        }
    };

    private void pullDownRequest() {
        IsPullDown = true;
        currentPage = 0;
        reqData(currentPage);
    }

    // 上拉刷新数据请求
    private void pullUpRequest() {
        //没有搜索结果 或者数据为空 上拉没有效果
        IsPullDown = false;
        currentPage++;
        reqData(currentPage);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager)
            manager.removeNoticeSystemListListener(this);

    }

    @Override
    public void onNoticeSystemListFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
        waittingLayout.setVisibility(View.VISIBLE);
        waittingProBar.setVisibility(View.GONE);
        waittingTV.setText(errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onNoticeSystemListSuccess(List<NoticeSystemData> list) {
        pullListView.onRefreshComplete(false, false);
        waittingLayout.setVisibility(View.GONE);

//        int pre = adapter.getList().size();
        if (null != list && list.size() > 0)
            if (IsPullDown) {
                adapter.clear();
            }
        {
            adapter.addData(list);
            adapter.notifyDataSetChanged();
//            if (0 == currentPage) { // 第一次时，默认定位到最后一条数据
//                mListView.setSelection(list.size() - 1);
//            } else {
//                mListView.setSelection(list.size());
//            }

        }

    }

    class MyAdapter extends BaseAdapter {

        private Context mContext;
        private List<NoticeSystemData> list = new ArrayList<NoticeSystemData>();
        private LayoutInflater mInflater;

        public MyAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(mContext);
        }

        public void clear() {
            if (null != list) {
                list.clear();
            }
        }

        // 下拉刷新 倒着加载数据
        public void addData(List<NoticeSystemData> addList) {
            for (int i = 0; i < addList.size(); i++) {
                list.add(addList.get(i));
            }
        }

        public List<NoticeSystemData> getList() {
            return list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.dn_notify_item, null);
                holder = new Holder();
                holder.createDateTV = convertView.findViewById(R.id.dn_notify_time);
                holder.contentTV = convertView.findViewById(R.id.dn_notify_content);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            NoticeSystemData data = list.get(position);
            holder.createDateTV.setText(DateUtils.getNewChatTime(data.getCreateDate()));
//            holder.createDateTV.setText(Utils.formatNotifyDate(data.getCreateDate()));
            holder.contentTV.setText(data.getContent() == null ? "" : data.getContent());
            // 要实现文字的点击效果，这里需要做特殊处理
            holder.contentTV.setMovementMethod(LinkMovementMethod.getInstance());
            holder.contentTV.setAutoLinkMask(Linkify.WEB_URLS);
            return convertView;
        }

        class Holder {
            public TextView createDateTV;
            public TextView contentTV;
        }


    }
}
